const T = require("./pub/tools");
const Base = require("./base");
const Drag = require("./libs/ccc.drag");

cc.Class({
    extends: Base,

    bindDrag( opt ){
       //将node 绑定拖拽事件
       const empty = ()=>{};
       let parasm = {};
       opt = opt || {};
       let  startHandler = opt.start && opt.start.handler ? opt.start.handler : empty,
            moveHandler = opt.move && opt.move.handler ? opt.move.handler : empty ,
            endHandler = opt.end && opt.end.handler ? opt.end.handler : empty ,
            cancelHandler = opt.cancel && opt.cancel.handler ? opt.cancel.handler : empty ;

       let startName = opt.start && opt.start.name ? opt.start.name : "touch-Start",
           moveName =  opt.move && opt.move.name ? opt.move.name : "touch-Move" ,
           endName =  opt.end && opt.end.name ? opt.end.name : "touch-End",
           cancelName = opt.cancel && opt.cancel.name ? opt.cancel.name : "touch-Cancel";

       this.node.on( startName , startHandler.bind(this) , this );
       this.node.on( moveName , moveHandler.bind(this) , this );
       this.node.on( endName , endHandler.bind(this) , this );
       this.node.on( cancelName , cancelHandler.bind(this) , this );

       this.__drag = new Drag( this.node , {
            startCheck : opt.start && opt.start.check ? opt.start.check : null,
            moveCheck : opt.move && opt.move.check ? opt.move.check : null,
            endCheck : opt.end && opt.end.check ? opt.end.check : null,
            cancelCheck : opt.cancel && opt.cancel.check ? opt.cancel.check : null,

            startEventName : startName,
            moveEventName : moveName,
            endEventName : endName,
            cancelEventName : cancelName,
       });
    },

    unbindDrag(){
        if(this.__drag){
            this.__drag.stop().then(()=>{
                this.__drag.unbind();
                this.__drag = null;
            })
        }
    },

    setSprite(spriteResName , node ){
        $RES.use(spriteResName)
        .then(( res )=>{
            node = node || this.node;
            T.setSprite(node , res );
        })
    }
});
